package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.orderservice.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p >
 *
 * @author testjava
 * @since 2021-04-17
 */
@Api(description = "订单支付")
//@CrossOrigin
@RestController
@RequestMapping("/orderservice/paylog")
public class TPayLogController {

    @Autowired
    private TPayLogService PayLogService;

    //生成二维码
    @ApiOperation(value = "生成二维码")
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = PayLogService.createNative(orderNo);
        return R.ok().data(map);
    }
    //查询订单状态
    @ApiOperation(value = "查询订单状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
        Map<String,String>map =PayLogService.queryPayStatus(orderNo);
        if(map==null){//出错
            return R.error().message("支付出错");
        }
        if("SUCCESS".equals(map.get("trade_state"))){
            //更改订单状态,插入支付日志
            PayLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(20005).message("支付中");
    }
}

